package controller

import (
	"60321/pkg/global"
	"60321/pkg/models"
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"
)

func GetNearlyCity(c *gin.Context) {
	var id = c.Param("id")
	var AdjacentCity []models.AdjacentCityModel
	if err := global.Db.Model(&models.AdjacentCityModel{}).
		Where("main_city_id = ?", id).
		Preload("AdjacentCityModel").
		Find(&AdjacentCity).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	var city []models.CityModel
	for _, v := range AdjacentCity {
		city = append(city, v.AdjacentCityModel)
	}
	var mainCity models.CityModel
	if err := global.Db.Take(&mainCity, "id = ?", id).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"city": city, "mainCity": mainCity})
}
func GetCity(c *gin.Context) {
	var city []models.CityModel
	if err := global.Db.Find(&city).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"city": city})
}
func GetCityTaxi(c *gin.Context) {
	var CityTaxis []models.CityTaxiModel
	var id = c.Param("id")
	global.Db.Model(&models.CityTaxiModel{}).
		Where("city_id = ?", id).
		Preload("TaxiModel").Find(&CityTaxis)
	var taxis []models.TaxiModel
	for _, v := range CityTaxis {
		taxis = append(taxis, v.TaxiModel)
	}
	c.JSON(http.StatusOK, gin.H{"taxi": taxis})
}

func GetUrbanCharacteristics(context *gin.Context) {
	var id = context.Param("id")
	var urbanCharacteristics string
	var city models.CityModel
	global.Db.Take(&city, "id = ?", id)
	urbanCharacteristics = city.UrbanCharacteristics
	var hotel = strings.Split(urbanCharacteristics, ",")[0]
	var scenicSpot = strings.Split(urbanCharacteristics, ",")[1]
	hotel = strings.Split(hotel, ":")[1]
	scenicSpot = strings.Split(scenicSpot, ":")[1]
	context.JSON(http.StatusOK, gin.H{"scenicSpot": scenicSpot, "hotel": hotel})
}
